﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Views;
using CommunitySystem.Ctrls;
using System.Collections.ObjectModel;
using CommunitySystem.PageView.晋升管理.新的晋升管理;
using CommunitySystem.EmpbasicService;
using CommunitySystem.JinShengService;
using EHR.Common;

namespace CommunitySystem.PageView
{
    public partial class NextPublicShowControl :  AsyncCtrl, IReturnListenerc
    {
        private JinShengService.JinShen_MainInfo _mainInfo = null;
        private ObservableCollection<JinShengService.JinShen_OKRenM_Detail> details = new ObservableCollection<JinShengService.JinShen_OKRenM_Detail>();

        private List<JinShengService.JinShen_PublicShow_Detail> gs = new List<JinShengService.JinShen_PublicShow_Detail>();

        private JinShengService.JinShengServiceClient _client = null;
        private bool CanEditInfo = true;
        private string Role = "";
        private List<JinShengService.JinShen_DangGongWei_Detail> dgw = new List<CommunitySystem.JinShengService.JinShen_DangGongWei_Detail>();

        private EmpbasicService.EmpBasicServiceClient Client2 = new EmpbasicService.EmpBasicServiceClient();
        public ObservableCollection<BianZhiModel> BianZhiModels = new ObservableCollection<BianZhiModel>();
    

        public NextPublicShowControl(JinShengService.JinShen_MainInfo mainInfo)
        {
            InitializeComponent();
            IsWorking.Visibility = Visibility.Visible;
            _mainInfo = mainInfo;
       
            TextBlock tb = new TextBlock() { Text = "科室:" + mainInfo.Department, Margin = new Thickness(10, 0, 0, 0), VerticalAlignment = System.Windows.VerticalAlignment.Center };
            selectionInfo.Children.Add(tb);
            tb = new TextBlock() { Text = "职位:" + mainInfo.Position + "(" + mainInfo.PositionLevel + ")", Margin = new Thickness(60, 0, 0, 0), VerticalAlignment = System.Windows.VerticalAlignment.Center };
            selectionInfo.Children.Add(tb);
            _client = new JinShengService.JinShengServiceClient();
            #region
            _client.GetOrgForHRCompleted += (sender, e) =>
            {
                Role = e.Result;
                if (JinShenConst.Administrator.Contains(e.Result))
                {

                    if (_mainInfo.Status == JinShenConst.STATUS_Promot_Apply)
                    {
                        btnConfirm.IsEnabled = true;
                        btnCheHui.IsEnabled = true;
                        btnSubmit.IsEnabled = false;
                    }
                    else
                    {
                        btnConfirm.IsEnabled = false;
                        btnCheHui.IsEnabled = false;
                        btnSubmit.IsEnabled = false;
                    }
                   
                }
                else
                {
                    if (_mainInfo.Status == JinShenConst.STATUS_Promot_Agree)
                    {
                        btnConfirm.IsEnabled = false;
                        btnCheHui.IsEnabled = false;
                        btnSubmit.IsEnabled = false;
                    }
                    if (_mainInfo.Status == "退回")
                    {
                        btnConfirm.IsEnabled = false;
                        btnCheHui.IsEnabled = false;
                        btnSubmit.IsEnabled = true;
                    }
                    if (_mainInfo.Status == JinShenConst.STATUS_Promot_Apply || _mainInfo.Status == JinShenConst.STATUS_Candidate_Show)
                    {
                        btnConfirm.IsEnabled = false;
                        btnCheHui.IsEnabled = false;
                        btnSubmit.IsEnabled = true;

                     
                    }
                }

                _client.GetJinJinShen_OKRenM_DetailAsync("MainID=" + _mainInfo.ID);
                
            };
            _client.GetOrgForHRAsync(EHR.Common.Session.LoginUser.LoginName);
            #endregion

            _client.GetJinShen_DangGongWei_DetailCompleted += (sender1, e1) =>
                {
                    dgw = e1.Result.ToList();
                };
            _client.GetJinShen_DangGongWei_DetailAsync("MainID=" + mainInfo.ID + " and IsAgree = '同意'");

            _client.GetJinJinShen_OKRenM_DetailCompleted += (sender, e) =>
            {
                details = e.Result;                
                _client.GetJinShen_PublicShow_DetailCompleted += (sender1, e1) =>
                {
                    gs = e1.Result.ToList();
                    if (details == null || details.Count <= 0)//新增
                    {
                        foreach (var d in e1.Result)
                        {
                            JinShengService.JinShen_OKRenM_Detail detail = new JinShengService.JinShen_OKRenM_Detail()
                            {
                                EmpID = d.EmpID,
                                EmpName = d.EmpName,
                                MainID = (int)d.MainID,
                            };
                            details.Add(detail);
                        }
                        
                    }
                    else//编辑
                    {
                        foreach (var d in e1.Result)
                        {
                            var newDetails = details.Where(c => (int)c.EmpID == d.EmpID).FirstOrDefault();
                            if (newDetails == null)//有新增同意的情况
                            {
                                JinShengService.JinShen_OKRenM_Detail detail = new JinShengService.JinShen_OKRenM_Detail()
                                {
                                    EmpID = d.EmpID,
                                    EmpName = d.EmpName,
                                    MainID = (int)d.MainID,
                                };
                                details.Add(detail);
                    
                            }
                        }
                      
                    }
                    if (details.Count == 0 || JinShenConst.Administrator.Contains(Role) || _mainInfo.Status == JinShenConst.STATUS_Promot_Agree) this.btnSubmit.IsEnabled = false;
                    else this.btnSubmit.IsEnabled = true;
                    Init();
                };
                _client.GetJinShen_PublicShow_DetailAsync("MainID=" + _mainInfo.ID + " and IsAgree=1 ");
                //}
            };

            //消息
            _client.AddSystemInfoCompleted += (ss, ee) =>
            {

            };
            _client.SaveJinShenMainInfoCompleted += (sender, e) =>
            {
                if (e.Error == null && _mainInfo.Status == JinShenConst.STATUS_Promot_Apply)
                {
                    _client.SaveJinShen_OKRenM_DetailAsync(details);
                }
                else if (e.Error == null && _mainInfo.Status == JinShenConst.STATUS_Promot_Agree)
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("确认成功!");

                    //添加消息
                    SystemInfo sysinfo = new SystemInfo();
                    sysinfo.MenuId = 376;
                    sysinfo.MoKuai = "晋升管理";
                    sysinfo.MoKuaiSX = "晋升办理";
                    sysinfo.DW1 = _mainInfo.OrgName;
                    sysinfo.CreateUser = Session.LoginUser.RealName;
                    sysinfo.CreateDate = DateTime.Now.ToString();
                    sysinfo.CreateUserID = 1;
                    sysinfo.OrgIDs = _mainInfo.OrgID.ToString();
                    sysinfo.LogUID = Session.LoginUser.UserID;
                    _client.AddSystemInfoAsync(sysinfo);
                }
            };
            _client.UpdateJinShenMStatusCompleted += (sender, e) =>
            {


            };
            _client.SaveJinShen_OKRenM_DetailCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    _client.UpdateJinShenMStatusAsync(_mainInfo.MId, _mainInfo.Status);
                    SilverlightMessageBoxLibrary.Message.InfoMessage("申请确认成功！");
                    //btnSubmit.IsEnabled = false;

                    //添加消息
                    SystemInfo sysinfo = new SystemInfo();
                    sysinfo.MenuId = 376;
                    sysinfo.MoKuai = "晋升管理";
                    sysinfo.MoKuaiSX = "晋升办理";
                    sysinfo.DW1 = _mainInfo.OrgName;
                    sysinfo.CreateUser = Session.LoginUser.RealName;
                    sysinfo.CreateDate = DateTime.Now.ToString();
                    sysinfo.CreateUserID = 1;
                    sysinfo.OrgIDs = _mainInfo.OrgID.ToString();
                    sysinfo.LogUID = Session.LoginUser.UserID;
                    _client.AddSystemInfoAsync(sysinfo);
                }
            };

            _client.ComfirmJinShenCompleted += (sender, e) =>
            {
                if (e.Result == true)
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("晋升成功!");
                    btnCheHui.IsEnabled = false;
                    btnConfirm.IsEnabled = false;
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("晋升失败!");
                }
            };

            //编制数
            Client2.GetBianZhiDetailModelCompleted += new EventHandler<GetBianZhiDetailModelCompletedEventArgs>(Client2_GetBianZhiDetailModelCompleted);
            Client2.GetBianZhiDetailModelAsync(mainInfo.OrgID.ToString(), "");
        }
        private ObservableCollection<JinShengService.JinShen_MainInfo> _mainInfos = null;
        private ObservableCollection<JinShengService.JinShen_MainInfo> SavemainInfos = null;
        public NextPublicShowControl(ObservableCollection<JinShengService.JinShen_MainInfo> mainInfos)
        {
            InitializeComponent();

            IsWorking.Visibility = Visibility.Visible;

            _mainInfos = mainInfos;
         
            gggg.Visibility = Visibility.Collapsed;
            _client = new JinShengService.JinShengServiceClient();
            #region
            _client.GetOrgForHRCompleted += (sender, e) =>
            {
                Role = e.Result;
                if (JinShenConst.Administrator.Contains(e.Result))
                {

                    if (_mainInfo.Status == JinShenConst.STATUS_Promot_Apply)
                    {
                        btnConfirm.IsEnabled = true;
                        btnCheHui.IsEnabled = true;
                        btnSubmit.IsEnabled = false;
                    }
                    else
                    {
                        btnConfirm.IsEnabled = false;
                        btnCheHui.IsEnabled = false;
                        btnSubmit.IsEnabled = false;
                    }

                }
                else
                {
                    if (mainInfos.First().Status == JinShenConst.STATUS_Promot_Agree)
                    {
                        btnConfirm.IsEnabled = false;
                        btnCheHui.IsEnabled = false;
                        btnSubmit.IsEnabled = false;
                    }
                    if (_mainInfo.Status == "退回")
                    {
                        btnConfirm.IsEnabled = false;
                        btnCheHui.IsEnabled = false;
                        btnSubmit.IsEnabled = true;
                    }
                    if (mainInfos.First().Status == JinShenConst.STATUS_Promot_Apply || mainInfos.First().Status == JinShenConst.STATUS_Candidate_Show)
                    {
                        btnConfirm.IsEnabled = false;
                        btnCheHui.IsEnabled = false;
                        if (details.Count == 0) this.btnSubmit.IsEnabled = false;
                        else this.btnSubmit.IsEnabled = true;
                    }
                }

                _client.GetJinJinShen_OKRenM_DetailAsync("MainID=" + _mainInfo.ID);

            };
            _client.GetOrgForHRAsync(EHR.Common.Session.LoginUser.LoginName);
            #endregion

            JSControl5 js5 = null;
            
            foreach (var mainInfo in mainInfos)
            {
                _mainInfo = mainInfo;
                if (mainInfo.DangGongWei_Detail.Where(p => p.MainID == mainInfo.ID).Where(p => p.IsAgree == "同意").ToList().Count!=0)
                dgw.Add(mainInfo.DangGongWei_Detail.Where(p => p.MainID == mainInfo.ID).Where(p => p.IsAgree == "同意").First());

                foreach (var d in mainInfo.PublicShow_Detail.Where(p => p.MainID == mainInfo.ID).ToList().Where(p => p.IsAgree == true).ToList())
                {
                    gs.Add(d);
                }
                if (mainInfo.OKRenM_Detail.Count != 0)
                {

                    foreach (var item in mainInfo.OKRenM_Detail)
                    {
                        details.Add(item);
                    }
                   
                }
                else
                {

                    foreach (var d in mainInfo.PublicShow_Detail.Where(p => p.MainID == mainInfo.ID).ToList().Where(p => p.IsAgree == true).ToList())
                    {
                        JinShengService.JinShen_OKRenM_Detail detail = new JinShengService.JinShen_OKRenM_Detail()
                        {
                            EmpID = d.EmpID,
                            EmpName = d.EmpName,
                            MainID = (int)d.MainID,
                        };
                        details.Add(detail);
                    }
                }
            

                js5 = new JSControl5(mainInfo, details);
                spDetails1.Children.Add(js5);
            }
           

            IsWorking.Visibility = Visibility.Collapsed;

            _client.SaveJinShenMainInfosStatusCompleted += (sender, e) =>
            {

                if (e.Error == null)
                {
                    SavemainInfos = e.Result[0].JinShen_MainInfos;
                    _client.SaveJinShen_OKRenM_DetailAsync(details);
                }
            };
            //消息
            _client.AddSystemInfoCompleted += (ss, ee) =>
            {

            };
            _client.SaveJinShen_OKRenM_DetailCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("申请确认成功！");
                    btnSubmit.IsEnabled = false;

                    //添加消息
                    SystemInfo sysinfo = new SystemInfo();
                    sysinfo.MenuId = 376;
                    sysinfo.MoKuai = "晋升管理";
                    sysinfo.MoKuaiSX = "晋升办理";
                    sysinfo.DW1 = _mainInfo.OrgName;
                    sysinfo.CreateUser = Session.LoginUser.RealName;
                    sysinfo.CreateDate = DateTime.Now.ToString();
                    sysinfo.CreateUserID = 1;
                    sysinfo.OrgIDs = _mainInfo.OrgID.ToString();
                    sysinfo.LogUID = Session.LoginUser.UserID;
                    _client.AddSystemInfoAsync(sysinfo);
                }
            };

            _client.ComfirmJinShensCompleted += (sender, e) =>
            {
                if (e.Result == true)
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("晋升成功!");
                    btnCheHui.IsEnabled = false;
                    btnConfirm.IsEnabled = false;
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("晋升失败!");
                }
            };

            //编制数
            if (mainInfos.Count != 0)
            {
                Client2.GetBianZhiDetailModelCompleted += new EventHandler<GetBianZhiDetailModelCompletedEventArgs>(Client2_GetBianZhiDetailModelCompleted);
                Client2.GetBianZhiDetailModelAsync(mainInfos.First().OrgID.ToString(), "");
            }
        }
        /// <summary>
        /// 编制数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Client2_GetBianZhiDetailModelCompleted(object sender, GetBianZhiDetailModelCompletedEventArgs e)
        {
            BianZhiModels = e.Result;
            if (BianZhiModels != null && BianZhiModels.Count > 0)
            {
                if (BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Count() > 0)
                {
                    foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true))
                    {
                        int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeLingDao) ? Convert.ToInt32(i.ZhengKeLingDao) : 0;
                        int renshu = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p => !string.IsNullOrEmpty(p.zhengkeshiyou) ? Convert.ToInt32(p.zhengkeshiyou) : 0);
                        int jieyong = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p =>
                            p.ZhengKeJieYongQingKuang != "无" ? Convert.ToInt32(p.ZhengKeJieYongQingKuang) : 0);
                        int chaozhi = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p =>
                            p.ZhengKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.ZhengKeChaoZhiQingKuang) : 0);

                        i.ZhengKeShiJi =
                            (zhiwhu - renshu + jieyong + chaozhi)
                                .ToString();
                    }
                }

                if (BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Count() > 0)
                {
                    foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true))
                    {
                        i.FuKeShiJi =
                            (Convert.ToInt32(
                                BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).OrderBy(p => p.FuKeShiJi).
                                    FirstOrDefault().FuKeShiJi) +
                             BianZhiModels.Sum(
                                 p => p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0) +
                             BianZhiModels.Sum(
                                 p => p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0))
                                .ToString();

                        int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeFeiLingDao) ? Convert.ToInt32(i.ZhengKeFeiLingDao) : 0;
                        int renshu = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p => !string.IsNullOrEmpty(p.fukeshiyou) ? Convert.ToInt32(p.fukeshiyou) : 0);
                        int jieyong = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p =>
                            p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0);
                        int chaozhi = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p =>
                            p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0);

                        i.ZhengKeShiJi =
                            (zhiwhu - renshu + jieyong + chaozhi)
                                .ToString();
                    }
                }
            }
        }
        public void BeginReturn()
        {
            
        }

        private void Init()
        {
            //党工委
           // var dgw = _mainInfo.DangGongWei_Detail.Where(p => p.MainID == _mainInfo.ID).ToList().Where(p => p.IsAgree == "同意").ToList();
           
            foreach (var d in details)
            {
                Border b = new Border() { BorderBrush = new SolidColorBrush(Colors.Blue), BorderThickness = new Thickness(1), Margin = new Thickness(5) };
                StackPanel sp = new StackPanel() { Margin = new Thickness(3), DataContext = d };
                b.Child = sp;
                StackPanel sp1 = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(3) };
                sp.Children.Add(sp1);

                var dgwxx = dgw.Where(p => p.EmpID == d.EmpID).First();


               TextBlock tb=new TextBlock(){Text="      ", Margin = new Thickness(0, 3, 0, 0)};
               sp1.Children.Add(tb);
               tb = new TextBlock() { Text = d.EmpName,Foreground=new SolidColorBrush(Colors.Blue) };
                sp1.Children.Add(tb);
                tb = new TextBlock() { Text = "同志任命为" };
                sp1.Children.Add(tb);
                tb = new TextBlock() { Text = _mainInfo.Department,Foreground=new SolidColorBrush(Colors.Blue) };
                sp1.Children.Add(tb);
                //tb = new TextBlock() { Text = "科" };
                //sp1.Children.Add(tb);
                //tb = new TextBlock() { Text = _mainInfo.Position + "(" + _mainInfo.PositionLevel+")", Foreground = new SolidColorBrush(Colors.Blue) };
                tb = new TextBlock() { Text = dgwxx.Position + "(" + dgwxx.AttributLevel + ")", Foreground = new SolidColorBrush(Colors.Blue) };
                sp1.Children.Add(tb);

                if (dgwxx.PositionAttibute == "领导职务")
                {
                    StackPanel sp3 = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(3) };
                    tb = new TextBlock() { Text = "任期自", Margin = new Thickness(0, 3, 0, 0) };
                    sp3.Children.Add(tb);
                    DatePicker dp = new DatePicker() { Margin = new Thickness(5, 0, 5, 0) };
                    dp.IsEnabled = CanEditInfo;
                    dp.SetBinding(DatePicker.SelectedDateProperty, new System.Windows.Data.Binding("StartDate") { Mode = System.Windows.Data.BindingMode.TwoWay, StringFormat = "yyyy-MM-dd" });
                    sp3.Children.Add(dp);
                    tb = new TextBlock() { Text = "到", Margin = new Thickness(0, 3, 0, 0) };
                    sp3.Children.Add(tb);
                    dp = new DatePicker() { Margin = new Thickness(5, 0, 5, 0) };
                    dp.IsEnabled = CanEditInfo;
                    dp.SetBinding(DatePicker.SelectedDateProperty, new System.Windows.Data.Binding("EndDate") { Mode = System.Windows.Data.BindingMode.TwoWay, StringFormat = "yyyy-MM-dd" });
                    sp3.Children.Add(dp);

                    StackPanel sp4 = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(3) };
                    tb = new TextBlock() { Text = "其中试用期", Margin = new Thickness(0, 3, 5, 0) };
                    TextBlock tbtb = new TextBlock() { Text = "年", Margin = new Thickness(0, 3, 5, 0) };
                    TextBox box1 = new TextBox() { Width = 60, Height = 25, DataContext = d };
                    box1.IsEnabled = CanEditInfo;
                    box1.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding("SYQYear") { Mode = System.Windows.Data.BindingMode.TwoWay });

                    sp4.Children.Add(tb);
                    sp4.Children.Add(box1);
                    sp4.Children.Add(tbtb);


                    sp.Children.Add(sp3);

                    sp.Children.Add(sp4);
                }
                else if (dgwxx.PositionAttibute == "非领导职务")
                {
                    StackPanel sp3 = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(3) };
                    tb = new TextBlock() { Text = "任命自", Margin = new Thickness(0, 3, 0, 0) };
                    sp3.Children.Add(tb);
                    DatePicker dp = new DatePicker() { Margin = new Thickness(5, 0, 5, 0) };
                    dp.IsEnabled = CanEditInfo;
                    dp.SetBinding(DatePicker.SelectedDateProperty, new System.Windows.Data.Binding("StartDate") { Mode = System.Windows.Data.BindingMode.TwoWay, StringFormat = "yyyy-MM-dd" });
                    sp3.Children.Add(dp);
                    tb = new TextBlock() { Text = "起", Margin = new Thickness(0, 3, 0, 0) };
                    sp3.Children.Add(tb);
                    //dp = new DatePicker() { Margin = new Thickness(5, 0, 5, 0) };
                    //dp.IsEnabled = CanEditInfo;
                    //dp.SetBinding(DatePicker.SelectedDateProperty, new System.Windows.Data.Binding("EndDate") { Mode = System.Windows.Data.BindingMode.TwoWay, StringFormat = "yyyy-MM-dd" });
                    //sp3.Children.Add(dp);

                    //StackPanel sp4 = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(3) };
                    //tb = new TextBlock() { Text = "其中试用期", Margin = new Thickness(0, 3, 5, 0) };
                    //TextBlock tbtb = new TextBlock() { Text = "年", Margin = new Thickness(0, 3, 5, 0) };
                    //TextBox box1 = new TextBox() { Width = 60, Height = 25, DataContext = d };
                    //box1.IsEnabled = CanEditInfo;
                    //box1.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding("SYQYear") { Mode = System.Windows.Data.BindingMode.TwoWay });

                    //sp4.Children.Add(tb);
                    //sp4.Children.Add(box1);
                    //sp4.Children.Add(tbtb);


                    sp.Children.Add(sp3);

                    //sp.Children.Add(sp4);
                }
               
               
              
                spDetails.Children.Add(b);
            }

            IsWorking.Visibility = Visibility.Collapsed;
        }


        /// <summary>
        /// 公示结果上报
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageButton_Click(object sender, RoutedEventArgs e)
        {
            if (Validate())
            {
                _mainInfo.Status = JinShenConst.STATUS_Candidate_Show;
                _client.SaveJinShenMainInfoAsync(_mainInfo);
            }
        }

        /// <summary>
        /// 申请晋升
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Click(object sender, RoutedEventArgs e)
        {
            if (Validate())
            {
                if (_mainInfos == null)
                {
                    _mainInfo.Status = JinShenConst.STATUS_Promot_Apply;
                    _client.SaveJinShenMainInfoAsync(_mainInfo);
                }
                else
                {
                    var Status = JinShenConst.STATUS_Promot_Apply;
                    _client.SaveJinShenMainInfosStatusAsync(_mainInfos, Status, _mainInfo.MId, EHR.Common.Session.LoginUser.LoginName);

                }
            }
        }

        /// <summary>
        /// 确认晋升
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            #region 编制数判断
            if (_mainInfos == null)
            {
                if (_mainInfo.PositionLevel == "乡科级正职（主任科员）" && _mainInfo.PositionAttibute == "领导职务")
                {
                    if (BianZhiModels.Where(p => p.OrgID == _mainInfo.OrgID && p.JiGouMingCheng == _mainInfo.Department && Convert.ToInt32(!string.IsNullOrEmpty(p.ZhengKeShiJi) ? p.ZhengKeShiJi : "0") > 0).FirstOrDefault() == null)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage(_mainInfo.Department+"科室没有对应的职数可用！");
                        return;
                    }
                }
                if (_mainInfo.PositionLevel == "乡科级副职（副主任科员）" && _mainInfo.PositionAttibute == "领导职务")
                {
                    if (BianZhiModels.Where(p => p.OrgID == _mainInfo.OrgID && p.JiGouMingCheng == _mainInfo.Department && Convert.ToInt32(!string.IsNullOrEmpty(p.FuKeShiJi) ? p.FuKeShiJi : "0") > 0).FirstOrDefault() == null)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage(_mainInfo.Department + "科室没有对应的职数可用！");
                        return;
                    }
                }
                if (_mainInfo.PositionLevel == "乡科级正职（主任科员）" && _mainInfo.PositionAttibute == "非领导职务")
                {
                    if (BianZhiModels.Where(p => p.OrgID == _mainInfo.OrgID && p.JiGouMingCheng == _mainInfo.Department && Convert.ToInt32(!string.IsNullOrEmpty(p.ZhuRenShiJi) ? p.ZhuRenShiJi : "0") > 0).FirstOrDefault() == null)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage(_mainInfo.Department + "科室没有对应的职数可用！");
                        return;
                    }
                }
                if (_mainInfo.PositionLevel == "乡科级副职（副主任科员）" && _mainInfo.PositionAttibute == "非领导职务")
                {
                    if (BianZhiModels.Where(p => p.OrgID == _mainInfo.OrgID && p.JiGouMingCheng == _mainInfo.Department && Convert.ToInt32(!string.IsNullOrEmpty(p.FuZhuRenShiJi) ? p.FuZhuRenShiJi : "0") > 0).FirstOrDefault() == null)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage(_mainInfo.Department + "科室没有对应的职数可用！");
                        return;
                    }
                }
            }
            else
            {
                #region 编制数判断
                foreach (var model in _mainInfos)
                {
                    if (model.PositionLevel == "乡科级正职（主任科员）" && model.PositionAttibute == "领导职务")
                    {
                        if (BianZhiModels.Where(p => p.OrgID == model.OrgID && p.JiGouMingCheng == model.Department && Convert.ToInt32(!string.IsNullOrEmpty(p.ZhengKeShiJi) ? p.ZhengKeShiJi : "0") > 0).FirstOrDefault() == null)
                        {
                            SilverlightMessageBoxLibrary.Message.ErrorMessage(model.Department+"科室没有对应的职数可用！");
                            return ;
                        }
                    }
                    if (model.PositionLevel == "乡科级副职（副主任科员）" && model.PositionAttibute == "领导职务")
                    {
                        if (BianZhiModels.Where(p => p.OrgID == model.OrgID && p.JiGouMingCheng == model.Department && Convert.ToInt32(!string.IsNullOrEmpty(p.FuKeShiJi) ? p.FuKeShiJi : "0") > 0).FirstOrDefault() == null)
                        {
                            SilverlightMessageBoxLibrary.Message.ErrorMessage(model.Department + "科室没有对应的职数可用！");
                            return ;
                        }
                    }
                    if (model.PositionLevel == "乡科级正职（主任科员）" && model.PositionAttibute == "非领导职务")
                    {
                        if (BianZhiModels.Where(p => p.OrgID == model.OrgID && p.JiGouMingCheng == model.Department && Convert.ToInt32(!string.IsNullOrEmpty(p.ZhuRenShiJi) ? p.ZhuRenShiJi : "0") > 0).FirstOrDefault() == null)
                        {
                            SilverlightMessageBoxLibrary.Message.ErrorMessage(model.Department + "科室没有对应的职数可用！");
                            return ;
                        }
                    }
                    if (model.PositionLevel == "乡科级副职（副主任科员）" && model.PositionAttibute == "非领导职务")
                    {
                        if (BianZhiModels.Where(p => p.OrgID == model.OrgID && p.JiGouMingCheng == model.Department && Convert.ToInt32(!string.IsNullOrEmpty(p.FuZhuRenShiJi) ? p.FuZhuRenShiJi : "0") > 0).FirstOrDefault() == null)
                        {
                            SilverlightMessageBoxLibrary.Message.ErrorMessage(model.Department + "科室没有对应的职数可用！");
                            return ;
                        }
                    }
                }
                #endregion
            }
            #endregion

            SilverlightMessageBoxLibrary.CustomMessage cm = new SilverlightMessageBoxLibrary.CustomMessage("确认同意晋升？", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
            cm.Show();
            cm.OKButton.Click -= new RoutedEventHandler(OKButton_Click);
            cm.OKButton.Click += new RoutedEventHandler(OKButton_Click);
        }

        void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if(_mainInfos==null)
            _client.ComfirmJinShenAsync(_mainInfo.ID, JinShenConst.STATUS_Promot_Agree);
            if (_mainInfos!= null)
                _client.ComfirmJinShensAsync(_mainInfos, JinShenConst.STATUS_Promot_Agree);
        }

        bool Validate()
        {

            if (details != null && details.Count > 0)
            {
                foreach (var item in details)
                {
                    var dgwxx = dgw.Where(p => p.EmpID == item.EmpID).First();
                    if (dgwxx.PositionAttibute == "领导职务")
                    {
                        if (item.StartDate == null || item.EndDate == null)
                        {
                            SilverlightMessageBoxLibrary.Message.ErrorMessage(item.EmpName + "确定任命的时间不能为空!");
                            return false;
                        }
                        if (item.StartDate > item.EndDate)
                        {
                            SilverlightMessageBoxLibrary.Message.ErrorMessage(item.EmpName + "结束时间不能早于开始时间!");
                            return false;
                        }
                    }
                    if (dgwxx.PositionAttibute == "非领导职务")
                    {
                        if (item.StartDate == null)
                        {
                            SilverlightMessageBoxLibrary.Message.ErrorMessage(item.EmpName + "确定任命的时间不能为空!");
                            return false;
                        }
                    }

                    foreach (var item1 in gs.Where(p => p.EmpID == item.EmpID).ToList())
                    {
                        if (item1.EndDate > item.StartDate)
                        {
                            SilverlightMessageBoxLibrary.Message.ErrorMessage(item.EmpName + "确定任命的开始时间不能早公示的结束时间!");
                            return false;
                        }
                    }
                }
            }
            return true;
        }

        private void btnCheHui_Click(object sender, RoutedEventArgs e)
        {
            _client.UpdateJinShenMStatusCompleted -= new EventHandler<JinShengService.UpdateJinShenMStatusCompletedEventArgs>(_client_UpdateJinShenMStatusCompleted);
            _client.UpdateJinShenMStatusCompleted += new EventHandler<JinShengService.UpdateJinShenMStatusCompletedEventArgs>(_client_UpdateJinShenMStatusCompleted);
            _client.UpdateJinShenMStatusAsync(_mainInfo.MId,"退回");
        }

        void _client_UpdateJinShenMStatusCompleted(object sender, JinShengService.UpdateJinShenMStatusCompletedEventArgs e)
        {
            if (e.Error == null && e.Result > 0)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("退回成功！");
          
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("退回失败！");
             
            }

        }
        
    }
}
